import pprint
pp = pprint.PrettyPrinter(indent=4)

def read_genome(filename=""):
    fp = open(filename)
    lines = fp.readlines()
    return lines[0].replace("\n", ""), int(lines[1].replace("\n", ""))


def frequency_table(text, k):
    freq_map = {}

    for i in range(0, len(text)-k+1):
        pattern = text[i: i+k]
        if pattern not in freq_map:
            freq_map[pattern] = 1
        else:
            freq_map[pattern] = freq_map[pattern] + 1

    return freq_map


def sorted_map(freq_map):
    return {k:v for k,v in sorted(freq_map.items(), key = lambda kv:kv[1])}


def max_map(freq_map):
    max_count = 0
    for k, v in freq_map.items():
        if v > max_count:
            max_count = v
    return max_count


def frequent_words(text, k):
    frequent_patterns = []

    freq_map = frequency_table(text, k)
    pp.pprint(freq_map)
    max_count = max_map(freq_map)
    for pattern in freq_map:
        if freq_map[pattern] == max_count:
            frequent_patterns.append(pattern)

    return frequent_patterns, max_count


if __name__ == "__main__":
    filename = "dataset_2_13.txt"
    text, k = read_genome(filename)
    # Week-1 quiz
    text, k = "CGCCTAAATAGCCTCGCGGAGCCTTATGTCATACTCGTCCT", 3
    freq_patterns, max_count = frequent_words(text, k)
    print(" ".join(freq_patterns))